package com.test.apps.cms.aslp;

import com.actionsoft.apps.cms.cache.CmsDataCache;
import com.actionsoft.apps.cms.cache.CmsModuleCache;
import com.actionsoft.apps.cms.model.CmsDataModel;
import com.actionsoft.apps.cms.model.CmsModuleModel;
import com.actionsoft.apps.cms.util.CmsAcPermUtil;
import com.actionsoft.apps.cms.util.CmsDataUtils;
import com.actionsoft.apps.cms.util.Page;
import com.actionsoft.apps.resource.interop.aslp.ASLP;
import com.actionsoft.apps.resource.interop.aslp.Meta;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.commons.security.ac.AccessControlAPI;
import com.actionsoft.bpms.commons.security.ac.model.AccessControlModel;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.UtilString;
import com.alibaba.fastjson.JSONArray;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class QueryCmsDataByModule
        implements ASLP
{
    @Override
    @Meta(parameter={"name: 'moduleid', required: true, desc: '栏目id'", "name: 'start', required: false, desc: '开始记录数'", "name: 'limit', required: false, desc: '每次加载的条数'", "name: 'isclose', required: false, desc: '发布状态 1已发布2未发布 '", "name: 'deleteflag', required: false, desc: '是否删除 1已删除 2未删除'", "name: 'returnIsRead', required: false, desc: '是否返回阅读状态 1不返回2返回'"})
    public ResponseObject call(Map<String, Object> params)
    {
        ResponseObject ro = ResponseObject.newOkResponse();
        try
        {
            if (params == null) {
                return ResponseObject.newErrResponse().err("不接受参数为空的调用");
            }
            String sid = (String)params.get("sid");
            String moduleid = (String)params.get("moduleid");
            UserContext ctx = UserContext.fromSessionId(sid);
            int start = params.get("start") == null ? 0 : ((Integer)params.get("start")).intValue();
            int limit = params.get("limit") == null ? -1 : ((Integer)params.get("limit")).intValue();
            String isclose = params.get("isclose") == null ? "1" : params.get("isclose").toString();
            String deleteflag = params.get("deleteflag") == null ? "1" : params.get("deleteflag").toString();
            String returnIsRead = params.get("returnIsRead") == null ? "1" : params.get("returnIsRead").toString();

            List<CmsDataModel> datalist = new ArrayList();
            if (UtilString.isEmpty(moduleid))
            {
                ro.err("不能接收参数为空的调用：栏目ID为空");
                return ro;
            }
            datalist = CmsDataCache.getAllMessageList(moduleid, isclose, deleteflag);

            Map<String, List<AccessControlModel>> acMap = CmsAcPermUtil.loadAcList();
            List<CmsDataModel> subList = new ArrayList();
            for (CmsDataModel cmsDataModel : datalist)
            {
                boolean readFlag = false;
                List<AccessControlModel> xxx = (List)acMap.get(cmsDataModel.getBoid());
                if (xxx == null) {
                    readFlag = true;
                } else {
                    readFlag = AccessControlAPI.getInstance().havingPermission(ctx.getUID(), xxx.iterator(), 0);
                }
                CmsModuleModel moduleModel = CmsModuleCache.getModel(cmsDataModel.getModuleId());
                if ((readFlag) && (moduleModel.getUseFlag().equals("1"))) {
                    subList.add(cmsDataModel);
                }
            }
            datalist = subList;
            if (limit > 0)
            {
                Object page = new Page(start, limit, datalist);
                datalist = ((Page)page).getListModel();
                ro.put("start", Integer.valueOf(((Page)page).getStart()));
                ro.put("limit", Integer.valueOf(((Page)page).getLimit()));
                ro.put("pageCount", Integer.valueOf(((Page)page).getPageCount()));
            }
            boolean flag = returnIsRead != "1";
            datalist = datalist.stream().sorted(Comparator.comparing(CmsDataModel::getCreateTime).reversed()).collect(Collectors.toList());
            JSONArray arrays = CmsDataUtils.data2JSON(datalist, ctx, flag);
            ro.put("datalist", arrays);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            ro = ResponseObject.newErrResponse();
            ro.err(e.getMessage());
        }
        return ro;
    }
}
